/*
 * s_common_msg.c
 *
 *  Created on: Sep 27, 2020
 *      Author: hemingway
 */
#include "s_common_evt.h"
#include <stdio.h>
#include <string.h>
#include "h_cc2640r2f_define.h"
#include "simple_peripheral_oad_offchip.h"

#include "s_common_ble.h"
#include "h_cc2640r2f_uart.h"

s_common_state s_common_evt_enqueue(s_common_evt_type type, \
                                               unsigned char *data, unsigned short len ,\
                                               unsigned char para)
{
    s_common_evt_t *pData = (s_common_evt_t *)H_CC2640R2F_DEBUG_MALLOC(sizeof(s_common_evt_t));

    if(NULL != pData) {
        pData->para = para;
        pData->evt_type = type;

        if(0 != len) {
            pData->len = len;
            pData->data = (unsigned char *)H_CC2640R2F_DEBUG_MALLOC(sizeof(unsigned char) * len);
            if(NULL != pData->data) {
                memcpy(pData->data, data, len);
            } else {
                pData->data = NULL;
                return s_common_enqueueMsg_failed;
            }
        } else {
            pData->data = NULL;
            pData->len = len;
        }
        if(SimplePeripheral_enqueueMsg(SBP_USER_EVT, 0, (unsigned char *)pData)) {
            return s_common_state_ok;
        } else {
            return s_common_enqueueMsg_failed;
        }
    }

    return s_common_malloc_failed;
}

s_common_state s_common_evt_handle_dequeued(s_common_evt_t *evt)
{
    switch(evt->evt_type) {
        case s_common_default_evt:
            break;

        case s_common_ble_evt:
            H_CC2640R2F_UART_TxBuff(evt->data, evt->len);
            break;

        case s_common_uart_evt:
//            printf("Get uart data\r\n");
            s_common_ble_send_bleData(evt->data, evt->len);
            break;

        default:
            break;
    }

    if(evt->data != NULL) {
        H_CC2640R2F_DEBUG_FREE(evt->data);
        evt->data = NULL;
    }

    return s_common_state_ok;
}
